$.fn.raty.defaults.starOn = "am-icon-star";
$.fn.raty.defaults.starOff = "am-icon-star-o";
var app = new EventsList(null, {
	api : ctx+'/weixin/vip/myEvaluatePage',
	params : {
		page : 1,
		no_record :'您当前还没有消费哦'
	}
}, initData);
app.init();

document.addEventListener('touchmove', function(e) {
	e.preventDefault();
}, false);

function initData(data, type, $list) {
	if (data.rows) {
		var temp = $.trim($("#record_temp").html());
		$.each(data.rows, function(i, item) {
			var date = new Date(item.creTime.replace(/-/g, "/"));
			var strDate = date.format("M月d日 EEE");
			var evaName = "去评价";
			if (item.isEvaluate == "10S")
				evaName = "已评价";
			var div = $(Comm.format(temp, strDate, item.serviceNames, item.mdseNames,
					item.employeeNames,evaName,item.orderInfoId));
			$btn = div.find('button');
			if (item.isEvaluate == "10S") {
				$btn.prop("disabled", true);
				$btn.addClass("am-btn-default");
				$btn.removeClass("am-btn-landiao");
			}else{
				$btn.click(function(){  
					evaluate(this,item.orderInfoId);  
				});  
			}
			div.appendTo($list);
		});
	}
}

var $modal = $('#doc-modal');
var $currentbtn;
function evaluate(btn,orderId) {
	$currentbtn = $(btn);
	$("#evaluateBtn").prop("disabled",true);
	var $sourcrDiv = $(btn).parents(".am-panel");
	var temp = $.trim($("#raty_temp").html());
	$modal.find(".employee-div").remove();
	$modal.find("#vipComment").val("");
	$("#orderInfoId").val(orderId);
	$.ajax({
		type : 'GET',
		url: ctx+"/weixin/vip/evaluate/"+orderId,
		dataType: "json",
		success: function (data) {
			$modal.find(".div-time").text($sourcrDiv.find(".div-time").text());
			$modal.find(".div-service").text($sourcrDiv.find(".div-service").text());
			$modal.find(".div-mdse").text($sourcrDiv.find(".div-mdse").text());
			var $commentDiv = $modal.find("#comment-div");
			var flag = false;
			$.each(data, function(i, item) {
				if(item.roleName != null && item.employeeId != null){
					var html = Comm.format(temp, i,item.orderInfoId,item.employeeId,item.employeeName,item.roleName== null ? "" :"（"+item.roleName+"）");
					$commentDiv.before(html);
					$("#raty-div-"+i).raty({
						starType : "i",
						score : 5,
						scoreName: 'scoreList['+i+'].score'
					});
					flag = true;
				}
			});
			if(flag){
				$(".div-mfs").removeClass("am-hide");
			}else{
				$(".div-mfs").addClass("am-hide");
			}
			$modal.modal();
			$("#evaluateBtn").prop("disabled",false);
		}
	});
}

$("#evaluateBtn").on("click",function(){
	var $btn = $(this);
	$btn.attr("disabled","disabled").text("正在提交...");

	$.ajax({
		type : 'POST',
		url: ctx+"/weixin/vip/evaluate",
		dataType: "json",
		data:$('#evaluateForm').serialize(),
		success: function (data) {
			Comm.msg(data.msg);
			$btn.prop("disabled",false).text("提交");
			if(data.success){
				$modal.modal('close');
				//app.init();
				$currentbtn.prop("disabled", true);
				$currentbtn.addClass("am-btn-default");
				$currentbtn.removeClass("am-btn-landiao");
				$currentbtn.text("已评价");
			}
		},
		error: function () {
			Comm.msg("评价失败!");
			$btn.prop("disabled",false).text("提交");
		}
	});
});